package com.th.supcom.pds.drugreturn.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.th.supcom.pds.common.client.commom.SimpleMethod;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 定义门诊退药处方表格
 * 
 * @author yzhang
 */
public class DrugReturnAndRefundPrescPanel extends LayoutContainer
{
    private CheckBoxSelectionModel <PdsPrescDetailInfoModel> m_checkmodel;
    /** 库存定义表. */
    private Grid <PdsPrescDetailInfoModel> m_grid;
    private ListStore <PdsPrescDetailInfoModel> m_store;

    public DrugReturnAndRefundPrescPanel ()
    {
        init ();
    }

    private ColumnModel createGridHeader (final CheckBoxSelectionModel <PdsPrescDetailInfoModel> chkModel)
    {
        final List <ColumnConfig> columns = new ArrayList <ColumnConfig> ();
        columns.add (chkModel.getColumn ());

        ColumnConfig column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "prescNo", "处方号", HorizontalAlignment.LEFT, 90, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "prescSourceName", "处方状态", HorizontalAlignment.LEFT, 60, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "repetition", "付数", HorizontalAlignment.LEFT, 40, null);
        columns.add (column);

        column = new ColumnConfig ();
        SimpleMethod.setColumnConfig (column, "totalPrice", "金额", HorizontalAlignment.LEFT, 60, getRenderForNumber ());
        columns.add (column);

        return new ColumnModel (columns);
    }

    public CheckBoxSelectionModel <PdsPrescDetailInfoModel> getCheckmodel ()
    {
        return m_checkmodel;
    }

    public Grid <PdsPrescDetailInfoModel> getGrid ()
    {
        return m_grid;
    }

    private GridCellRenderer <PdsPrescDetailInfoModel> getRenderForNumber ()
    {
        return new GridCellRenderer <PdsPrescDetailInfoModel> ()
        {

            @Override
            public Object render (final PdsPrescDetailInfoModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsPrescDetailInfoModel> store, final Grid <PdsPrescDetailInfoModel> grid)
            {
                final Double modelname = model.getTotalPrice ();
                return "<span>" + ClientUtils.formatNumTwo (modelname) + "</span>";
            }
        };
    }

    public ListStore <PdsPrescDetailInfoModel> getStore ()
    {
        return m_store;
    }

    /**
     * 初始化UI
     */
    private void init ()
    {
        setBorders (true);
        setLayout (new FitLayout ());
        m_checkmodel = new CheckBoxSelectionModel <PdsPrescDetailInfoModel> ();
        m_checkmodel.setSelectionMode (SelectionMode.MULTI);
        m_store = new ListStore <PdsPrescDetailInfoModel> ();
        m_grid = new Grid <PdsPrescDetailInfoModel> (m_store, createGridHeader (m_checkmodel));
        m_grid.getView ().setEmptyText ("暂时没有您需要的数据...");
        m_grid.setSelectionModel (m_checkmodel);
        m_grid.setStripeRows (true);
        m_grid.setColumnLines (true);
        m_grid.setColumnReordering (true);
        m_grid.addPlugin (m_checkmodel);
        add (m_grid);
    }

    public final void refresh (final PdsPrescDetailInfoModel detail)
    {
        m_grid.getStore ().removeAll ();
        if (null != detail)
        {
            m_grid.getStore ().add (detail);
        }
    }

}
